/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.core;

import com.blandware.atleap.common.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Represents template of mail message. This is a special {@link Localizable}
 * which defines some internal fields (from, subject, body, charset) that will be
 * used when preparing mail message.
 * </p>
 * <p>
 * Those may contain variables like this: $message.
 * </p>
 * <p>
 * Also, mime type is specified for template. It defines mime type of generated
 * mail messages. This can be 'text/html' or 'text/plain'.
 * </p>
 * <p><a href="MailTemplate.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.14 $ $Date: 2006/08/03 10:07:34 $
 * @struts.form include-all="false" extends="BaseForm"
 * @hibernate.joined-subclass table="`al_core_mail_template`" lazy="false"
 * @hibernate.joined-subclass-key column="`localizable_id`"
 */
public class MailTemplate extends Localizable {

	//~ Instance variables

    /**
     * The identifier of this template
     */
	protected String identifier;
    /**
     * Mime-type of this template
     */
	protected String mimeType = Constants.MIME_TYPE_PLAIN;

	/**
	 * List of available variables to use in template body. It is taken from configuration file and is not persistent.
	 */
	protected List availableVariables = new ArrayList();

	/**
	 * Value of <code>from</code> field in mail message
	 */
	protected Map from = new HashMap();

	/**
	 * Message subject
	 */
	protected Map subject = new HashMap();

	/**
	 * Template body
	 */
	protected Map body = new HashMap();

	/**
	 * Template charset
	 */
	protected Map charset = new HashMap();

	//~ Constructors

	/**
	 * Creates new instance of MailTemplate
	 */
	public MailTemplate() {
	}

	//~ Methods

	/**
     * Gets identifier of this mail template
     *
     * @return the identifier
	 * @hibernate.property column="`identifier`" not-null="true" unique="true" length="252"
	 */
	public String getIdentifier() {
		return identifier;
	}

    /**
     * Sets identifier of this mail template
     *
     * @param identifier the identifier to set
     */
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	/**
     * Gets mime-type of messages that are based on this mail template
     *
	 * @return mime-type
	 * @struts.form-field
	 * @hibernate.property column="`mime_type`" not-null="true" unique="false" length="252"
	 */
	public String getMimeType() {
		return mimeType;
	}

    /**
     * Sets mime-type of messages that are based on this mail template
     *
     * @param mimeType the mime-type to set
     */
	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	/**
	 * Returns <code>true</code>, if mime-type of this template is text/plain
     *
	 * @return <code>true</code> if this template is plain
	 */
	public boolean isPlain() {
		return this.mimeType.equalsIgnoreCase(Constants.MIME_TYPE_PLAIN);
	}

	// Internal field values

	/**
     * Gets 'from' value (it's used in messages that are based on this template).
     * This is a map from locale identifiers to strings that are specific to
     * locales.
     *
	 * @return map of 'from' values
	 * @struts.form-field
	 */
	public Map getFrom() {
		return from;
	}

    /**
     * Sets 'from' map
     *
     * @param from map of 'from' values to set
     * @see MailTemplate#getFrom()
     */
	public void setFrom(Map from) {
		this.from = from;
	}

	/**
     * Gets map of subjects that will be used in messages that are based on this
     * template. This is a map from locale identifiers to strings that are specific to
     * locales.
     *
	 * @return subject map of 'subject' values
	 * @struts.form-field
	 */
	public Map getSubject() {
		return subject;
	}

    /**
     * Sets map of 'subject' values
     *
     * @param subject map of 'subject' values
     * @see MailTemplate#getSubject()
     */
	public void setSubject(Map subject) {
		this.subject = subject;
	}

	/**
     * Gets map of bodies that will be used in messages that are based on this
     * template. This is a map from locale identifiers to bodies that are specific to
     * locales.
     *
	 * @return map of 'body' values
	 * @struts.form-field
	 */
	public Map getBody() {
		return body;
	}

    /**
     * Sets map of 'body' values
     *
     * @param body map of 'body' values
     * @see MailTemplate#getBody()
     */
	public void setBody(Map body) {
		this.body = body;
	}

	/**
     * Gets map of charsets that will be used in messages that are based on this
     * template. This is a map from locale identifiers to charsets that are specific to
     * locales.
     *
	 * @return map from locale identifiers to charsets
	 * @struts.form-field
	 */
	public Map getCharset() {
		return charset;
	}

    /**
     * Sets map of charsets
     *
     * @param charset map from locale identifiers to charsets
     * @see MailTemplate#getCharset()
     */
	public void setCharset(Map charset) {
		this.charset = charset;
	}

    /**
     * Gets list of variables that can be used in template body
     *
     * @return list of variables
     */
	public List getAvailableVariables() {
		return availableVariables;
	}

    /**
     * Sets list of variables that can be used in template body
     *
     * @param availableVariables list of variables
     */
	public void setAvailableVariables(List availableVariables) {
		this.availableVariables = availableVariables;
	}

	public boolean equals(Object o) {
		if ( this == o ) {
			return true;
		}
		if ( !(o instanceof MailTemplate) ) {
			return false;
		}

		final MailTemplate mailTemplate = (MailTemplate) o;

		if ( identifier != null ? !identifier.equals(mailTemplate.identifier) : mailTemplate.identifier != null ) {
			return false;
		}

		return true;
	}

	public int hashCode() {
		return (identifier != null ? identifier.hashCode() : 0);
	}

}

